<?php

	 function getOrganizationType($id){
	 	$sql="SELECT `type` FROM `organization` WHERE id=$id";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		return $row['type'];
	 }
   function getLogicValue($para){
   		$sql="SELECT value FROM `_c_logic` WHERE para='$para'";
		$result = mysql_query($sql);
		$row = mysql_fetch_array($result);
		return $row['value'];
   }
   function getReportName(){
	$sql="SELECT * 
	FROM  `_c_logic` 
	WHERE  `name` =  'reportType'";
	$result = mysql_query($sql);
	    $return = array ();
        while($row = mysql_fetch_array($result))
        {
        	$para=$row['para'];
        	$return[$para]=$row['value'];
        }
		return $return;
   }
   
  function storedProcedure($spname,$parameters)
	{
		$sql="CALL `$spname` (";
		foreach($parameters as $p){
			if(substr($p,0,1)=='@'){
				$sql.=$p.",";
			}else{
			$sql.="'".$p."',";
			}
		}
		if(count($parameters)>0){
		$sql=substr($sql, 0, -1); 
		}
		$sql.=")";
		echo $sql;
		
		return mysql_query($sql);
		//$result = mysql_query($name);
	}
function getDefaultPriceByInv_id($id){
	$sql="SELECT scan_code FROM inventory WHERE id='$id'";
	echo "getDefaultPriceByInv_id:".$sql."<br>";
	$sqlresult=mysql_query($sql);
	if(mysql_num_rows($sqlresult)==0){
		return 0;
	}
	$scan_code =mysql_result($sqlresult, 0);
	return getDefaultPriceByScan_code($scan_code);
	
}

function getDefaultPriceByScan_code($scan_code){
	$sql="SELECT price FROM item WHERE scan_code='$scan_code'";
	echo "getDefaultPriceByScan_code:".$sql."<br>";
	$sqlresult=mysql_query($sql);
	if(mysql_num_rows($sqlresult)==0){
		return 0;
	}
    return mysql_result($sqlresult, 0);
	
}
function getInv_id($source,$scan_code){
	
	$sql="SELECT id FROM inventory WHERE orid='$source' AND scan_code='$scan_code' ";
	echo "getInv_id:".$sql."<br>";
	$sqlresult=mysql_query($sql);
	if(mysql_num_rows($sqlresult)==0){
		return 0;
	}
	$result =mysql_result($sqlresult, 0);
	return $result;
	
	
	
}
function inventory_limit($inv_id){
	$sql="SELECT number FROM inventory WHERE id='$inv_id'";
	$sqlresult=mysql_query($sql);
	$result =mysql_result($sqlresult, 0);
	if(is_numeric($result)){
		return $result;
	}else{
		return 0;
	}
	// outputs third employee's name
	
}

function inventory_scan_code($inv_id){
	$sql="SELECT scan_code FROM inventory WHERE id='$inv_id'";
	echo $sql."<br>";
	$sqlresult=mysql_query($sql);
	$result =mysql_result($sqlresult, 0);
	if(isset($result)){
		return $result;
	}else{
		return 0;
	}
	// outputs third employee's name
	
}




function isItemExist($scan_code){
	
	$sql="SELECT * FROM item WHERE scan_code='$scan_code'";
	    $result=mysql_query($sql);
	$num_rows = mysql_num_rows($result);
	if($num_rows==0){
		return 0;
	}else{
		return 1;
	}
}

function alterItem($scan_code,$designer,$name,$active,$price,$ratio){
	$sql="UPDATE  `backoffice`.`item` SET  `designer` =  '$designer',`name` =  '$name',`active` =  '$active',`price` =  '$price',`ratio` =  '$ratio' WHERE  `item`.`scan_code` =  '$scan_code'";
	return mysql_query($sql);
}
function createItem($scan_code,$designer,$name,$active,$price,$ratio){
	
		$product_code=substr($scan_code, 0,10);
    	$designer_code=$designer.substr($scan_code, 0,2).substr($scan_code, 6,3);
    	$type=substr($scan_code, 0,2);
    	$year=substr($scan_code, 2,3);
    	$season=substr($scan_code, 5,1);
    	$flow=substr($scan_code, 6,3);
    	$material=substr($scan_code, 9,1);
    	$color=substr($scan_code, 10,2);
    	$size=substr($scan_code, 12,2);
        $sql="INSERT INTO  `backoffice`.`item` (
             `scan_code` ,
             `product_code` ,
             `designer_code` ,
             `type` ,
             `year` ,
             `season` ,
             `flow` ,
             `material` ,
             `color` ,
             `size` ,
             `designer` ,
             `name` ,
             `active` ,
             `price` ,
             `ratio`
             )
             VALUES (
'$scan_code',  '$product_code',  '$designer_code',  '$type',  '$year',  '$season',  '$flow',  '$material',  '$color',  '$size',  '$designer',  '$name' ,'$active',  '$price',  '$ratio'
             )";
			 echo $sql."<br>";
		return mysql_query($sql);
		
}



function addItem($Orid,$scan_code,$uid,$number){//for stack 
	echo $Orid."+".$scan_code."+".$uid."+".$number;
	$sql="SELECT  `number` 
FROM  `inventory` 
WHERE  `scan_code`='$scan_code' AND `orid` ='$Orid'";
	    $result=mysql_query($sql);
	    echo "<br> result=".$result;
	$num_rows = mysql_num_rows($result);
	echo "<br>num_rows=".$num_rows;
    if($num_rows==1){
    	$sql="UPDATE  `backoffice`.`inventory` SET  `number` =  `number`+$number,`time_stamp`=now() WHERE  `scan_code`='$scan_code' AND `inventory`.`orid` =$Orid";
	
    }else{
   
	$sql="INSERT INTO  `backoffice`.`inventory` (
`id` ,
`orid` ,
`scan_code` ,
`number` ,
`time_stamp`
)
VALUES ('',
'$Orid',  '$scan_code',  '$number', 
CURRENT_TIMESTAMP
)";

	 }	
		
					echo $sql;
		
		
		$result=mysql_query($sql);
					echo "<br>after:".$sql;
		echo "<br>result:".$result;
		if($result){
			//return 1;
			$return=record('add',$Orid,$Orid,$scan_code,1,$uid,$uid,$number,0);
			echo "record add=>".$return."<br>";
			return $return;
		}else{
			echo $sql;
			return 0;
        }
        
  }


function returnItem($Orid,$scan_code,$uid,$number,$amount){
	echo $Orid."+".$scan_code."+".$uid."+".$number."amount:".$amount;
	$sql="SELECT  `number` 
FROM  `inventory` 
WHERE  `scan_code`='$scan_code' AND `orid` ='$Orid'";
	    $result=mysql_query($sql);
	    echo "<br> result=".$result;
	$num_rows = mysql_num_rows($result);
	echo "<br>num_rows=".$num_rows;
    if($num_rows==1){
    	$sql="UPDATE  `backoffice`.`inventory` SET  `number` =  `number`+$number,`time_stamp`=now() WHERE  `scan_code`='$scan_code' AND `inventory`.`orid` =$Orid";
	
    }else{
   
	$sql="INSERT INTO  `backoffice`.`inventory` (
`id` ,
`orid` ,
`scan_code` ,
`number` ,
`time_stamp`
)
VALUES ('',
'$Orid',  '$scan_code',  '$number', 
CURRENT_TIMESTAMP
)";

	 }	
		
					echo $sql;
		
		
		$result=mysql_query($sql);
					echo "<br>after:".$sql;
		echo "<br>result:".$result;
		if($result){
			//return 1;
			$return=record('return',"0",$Orid,$scan_code,1,"0",$uid,$number,$amount);
			echo "record return=>".$return."amount= ".$amount."<br>";
			return $return;
		}else{
			echo $sql;
			return 0;
        }
        
  }
function updateRecord($tran_id,$user1){
				$sql="UPDATE `backoffice`.`transaction` SET  `confirm` =  '1', `user2`='$user1',`finish_time_stamp`=CURRENT_TIMESTAMP WHERE `id`='$tran_id'";
			echo "updateRecord".$sql."<br>";
		
		$result=mysql_query($sql);
	return $result;
	
}
			

  function record($action,$source_Orid,$target_Orid,$scan_code,$comfirm,$uid1,$uid2,$number,$income,$code){
  			echo "code is".$code."<br>";
  		if($code==""){
  			$code='无';
  		}
  	if($action=='add'||$action=='sell'||$action=='return'||$action=='consume'){
  		$sql="INSERT INTO  `backoffice`.`transaction` (
`id` ,
`scan_code` ,
`source` ,
`target` ,
`action` ,
`confirm` ,
`user1` ,
`user2` ,
`amount` ,
`income` ,
`begin_time_stamp`, 
`finish_time_stamp`,
`code`
)
VALUES (
'',  '$scan_code',  '$source_Orid',  '$target_Orid',  '$action',  '$comfirm',  '$uid1', '$uid2', '$number',  '$income', 
CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,'$code'
)";
			}else{
			
  	$sql="INSERT INTO  `backoffice`.`transaction` (
`id` ,
`scan_code` ,
`source` ,
`target` ,
`action` ,
`confirm` ,
`user1` ,
`user2` ,
`amount` ,
`income` ,
`begin_time_stamp`, 
`finish_time_stamp`,
`code`
)
VALUES (
'',  '$scan_code',  '$source_Orid',  '$target_Orid',  '$action',  '$comfirm',  '$uid1', '$uid2', '$number',  '$income', 
CURRENT_TIMESTAMP, '0000-00-00 00:00:00','$code'
)";
			}
echo "<br>recording .sql = $sql";
	$result=mysql_query($sql);
	echo "<br>result =$result";
	return $result;
  }
  function readRecord($tran_id){
  	$sql="SELECT * FROM `backoffice`.`transaction` WHERE `id`='$tran_id'";
  	//echo $sql;
  		$result=mysql_query($sql);
		$row = mysql_fetch_array($result);
		
		//var_dump($result);
	return $row;
  }
  
function distributeItem($inv_id,$scan_code,$limit,$number,$source,$target,$user1){
	$update= $limit-$number;
	$sql="UPDATE  `backoffice`.`inventory` SET  `number` =  '$update' WHERE  `inventory`.`id` =$inv_id";
	echo "distributeItem: ".$sql."<br>";
	if(mysql_query($sql)){
		echo "sql success";
		echo "record('distribute',$source,$target,$scan_code,0,$user1,'',$number,0);<br>";
		return record('distribute',$source,$target,$scan_code,0,$user1,'',$number,0);
		
	}else{
		return 0;
	}
		
	
}
function sellItem($inv_id,$scan_code,$limit,$number,$source,$amount,$user1,$code){
	echo "code is".$code."<br>";
	$update= $limit-$number;
	$sql="UPDATE  `backoffice`.`inventory` SET  `number` =  '$update' WHERE  `inventory`.`id` =$inv_id";
	echo "sellItem: ".$sql."<br>";
	if(mysql_query($sql)){
		echo "inventory update sql success";
		return record('sell',$source,'0',$scan_code,1,$user1,'0',$number,$amount,$code);
		
	}else{
		return 0;
	}
		
}


  function distributeConfirm($user1,$tran_id,$Orid){
  			
  		echo "distributeConfirm".$user1.$tran_id.$Orid;
  	$array=readRecord($tran_id);

  	if(is_array($array)){
  		
		$scan_code=$array['scan_code'];
		$number=$array['amount'];
		
		
  	}else{
  		return 0;
  	}

  	
  	$sql="SELECT * FROM `backoffice`.`inventory` WHERE `scan_code`='$scan_code' AND `orid`=$Orid";
	echo $sql."<br>";
	$result=mysql_query($sql);
		if(mysql_num_rows($result)){
			$sql="UPDATE `backoffice`.`inventory` SET  `number` =  `number`+$number WHERE `scan_code`='$scan_code'AND `orid`=$Orid";
		}else{
			$sql="INSERT INTO  `backoffice`.`inventory` (
`id` ,
`orid` ,
`scan_code` ,
`number` ,
`time_stamp`
)
VALUES ('',
'$Orid',  '$scan_code',  '$number', 
CURRENT_TIMESTAMP
)";
		}
	
  	
  				
  			
  		echo "distribute choose:".$sql."<br>";
  			$result=mysql_query($sql);
  			if($result){
  				$result= updateRecord($tran_id,$user1);
  			}else{
return 0;
  		    }
  		return $result;
  	
  	
  }


?>